
#include <vector>
#include <unistd.h>
#include <string>
#include <iostream>

class task
{
public:
    task(){}
    task(int x, int y, char op)
        : _num_x(x), _num_y(y), _operator(op)
    {}

    std::string operator()()
    {
        int rt_val = -1;
        switch (_operator)
        {
        case '+':
            rt_val = _num_y + _num_x;
            break;

        case '-':
            rt_val = _num_x - _num_y;
            break;

        case '*':
            rt_val = _num_x * _num_y;
            break;

        case '/':
        {
            if (_num_y == 0)
                std::cout << "div zero error" << std::endl;
            else
                rt_val = _num_x / _num_y;
            break;
        }

        case '%':
        {
            if (_num_y == 0)
                std::cout << "mod zero error" << std::endl;
            else
                rt_val = _num_x % _num_y;
            break;
        }

        default:
            std::cout << "operator not support" << std::endl;
            break;
        }

        char buff[64];
        snprintf(buff, sizeof(buff) - 1, "%d %c %d = %d", _num_x, _operator, _num_y, rt_val);
        std::cout << "这是在处理函数内部， 处理结果：" << buff << std::endl;
        return buff;
    }

    std::string task_name()
    {
        char buff[64];
        snprintf(buff, sizeof buff, "%d %c %d = ?", _num_x, _operator, _num_y);
        return buff;
    }

private:
    int _num_x;
    int _num_y;
    char _operator;
};

#include "C++_queue_cond.hpp"

void *start_push(void *args)
{
    char op[5] = {'+', '-', '*', '/', '%'};
    queue_cond<task> *test = (queue_cond<task> *)args;
    
    

    while (true)
    {
        int x = rand() % 100;
        int y = rand() % 30;
        task _test(x, y, op[rand() % 5]);
        
        test->push(_test);
        std::cout << "已载入数据" << _test.task_name() << std::endl;
    }
    return nullptr;
}

void *start_pop(void *args)
{
    queue_cond<task> *test = (queue_cond<task> *)args;
    while (true)
    {
        std::string result = test->pop()();
        std::cout << "已拿出数据：" << result << std::endl;
        usleep(1000);
    }
    return nullptr;
}

int main()
{
    std::vector<pthread_t> threads(4);
    queue_cond<task> qc;
    pthread_create(&threads[0], nullptr, start_push, (void *)&qc);
    pthread_create(&threads[1], nullptr, start_push, (void *)&qc);
    pthread_create(&threads[2], nullptr, start_pop, (void *)&qc);
    pthread_create(&threads[3], nullptr, start_pop, (void *)&qc);

    for (int i = 0; i < threads.size(); i++)
    {
        pthread_join(threads[i], nullptr);
    }

    return 0;
}